<template>
  <div class="permission-container">
    <div class="app-container">
      <!-- 表格 -->
      <el-card>
        <div style="text-align: right; margin-bottom: 20px">
          <el-button type="primary" size="small" @click="AddRole(1,'0')">添加权限</el-button>
        </div>
        <el-table border :data="list" row-key="id">
          <el-table-column label="名称" prop="name" />
          <el-table-column label="标识" prop="code" />
          <el-table-column label="描述" prop="description" />
          <el-table-column label="操作">
            <template #default="{row}">
              <el-button v-if="row.type===1" size="small" type="text" @click="AddRole(2,row.id)">添加权限点</el-button>
              <el-button size="small" type="text" @click="clickShowEdit(row.id)">查看</el-button>
              <el-button size="small" type="text" @click="del(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
      <!-- 新增编辑的弹层 -->
      <el-dialog :title="showTitle" :visible="showDialog" @close="closeDialog">
        <!-- 表单内容 -->
        <el-form label-width="100px">
          <el-form-item label="权限名称">
            <el-input v-model="formData.name" />
          </el-form-item>
          <el-form-item label="权限标识">
            <el-input v-model="formData.code" />
          </el-form-item>
          <el-form-item label="权限描述">
            <el-input v-model="formData.description" />
          </el-form-item>
          <el-form-item label="权限启用">
            <el-switch
              v-model="formData.enVisible"
              active-text="启用"
              active-value="1"
              inactive-text="不启用"
              inactive-value="0"
            />
          </el-form-item>
        </el-form>

        <template #footer>
          <div style="text-align: right;">
            <el-button @click="closeDialog">取消</el-button>
            <el-button type="primary" @click="clickSubmit">确定</el-button>
          </div>
        </template>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { transformDepartments } from '@/utils/index'
import {
  reqGetPermissionList,
  reqDelPermission,
  reqAddPermission,
  reqGetPermissionDetail,
  reqUpdatePermission
} from '@/api/permisson'
export default {
  name: 'Permission',
  data() {
    return {
      list: [],
      showDialog: false, // 弹层显示布尔值
      formData: {
        enVisible: '0', // 开启
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        type: '', // 类型
        pid: '' // 添加到哪个节点下
      }
    }
  },
  computed: {
    showTitle() {
      if (this.formData.id) {
        return '编辑提交'
      } else {
        return '添加提交'
      }
    }
  },
  created() {
    this.getPermissionList()
  },
  methods: {
    async getPermissionList() {
      const { data } = await reqGetPermissionList()
      // console.log(res)
      this.list = transformDepartments(data, '0')
      // console.log(this.list)
    },
    // 删除权限
    del(id) {
      this.$confirm('确定删除吗', {
      }).then(async() => {
        const { message } = await reqDelPermission(id)
        // console.log(res)
        this.$message.success(message)
        // 重新渲染页面
        this.getPermissionList()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 添加权限
    AddRole(type, pid) {
      this.showDialog = true
      // console.log(type, pid)
      this.formData.type = type
      this.formData.pid = pid
    },
    // 关闭弹层
    closeDialog() {
      this.showDialog = false
      this.formData = {
        enVisible: '0', // 开启
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        type: '', // 类型
        pid: '' // 添加到哪个节点下
      }
    },
    // 确定按钮
    async clickSubmit() {
      if (this.formData.id) {
        // 编辑提交
        const { message } = await reqUpdatePermission(this.formData)
        this.$message.success(message)
      } else {
        // 添加提交
        if (this.formData.name.trim().length === 0 || this.formData.code.trim().length === 0) {
          return this.$message.error('请输入名称或权限标识')
        }
        const res = await reqAddPermission(this.formData)
        console.log(res)
        this.$message.success('添加成功')
      }
      // 重新渲染页面
      this.getPermissionList()
      this.showDialog = false
    },
    // 查看
    async clickShowEdit(id) {
      const res = await reqGetPermissionDetail(id)
      console.log(res)
      this.formData = res.data
      this.showDialog = true
    }
  }
}
</script>
